package com.hn658.user.backend.action.appinfo.impl;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.ws.rs.Consumes;
import javax.ws.rs.FormParam;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Context;

import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.time.DateUtils;
import org.apache.cxf.jaxrs.ext.multipart.Multipart;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

import com.hn658.framework.common.AbstractBaseService;
import com.hn658.framework.common.IServiceResponse;
import com.hn658.framework.common.ResponseStatus;
import com.hn658.framework.dataaccess.pagination.GenericPage;
import com.hn658.framework.shared.exception.BusinessException;
import com.hn658.user.appinfo.service.IAppInfoService;
import com.hn658.user.backend.action.appinfo.IAppInfoAction;
import com.hn658.user.backend.action.appinfo.vo.AppInfoRequest;
import com.hn658.user.backend.action.appinfo.vo.QueryAppInfoResponse;
import com.hn658.user.common.entity.AppInfoEO;
import com.hn658.user.common.utils.DateUtil;

/**
 * Created by baihai on 2015/8/26.
 */
@Controller("appInfoAction")
@Path("appinfo")
@Produces("application/json; charset=UTF-8")
@Consumes("application/json; charset=UTF-8")
public class AppInfoActionImpl extends AbstractBaseService implements IAppInfoAction{


    @Autowired
    private IAppInfoService appInfoService;

    @Path("queryAppInfo")
    @Consumes("application/x-www-form-urlencoded; charset=UTF-8")
    @Produces("application/json; charset=UTF-8")
    @POST
    public IServiceResponse queryAppInfo(
            @FormParam(value="appId") String appId,
            @FormParam(value="appName") String appName,
            @FormParam(value="createStartTime") String createStartTime,
            @FormParam(value="createEndTime") String createEndTime,
            @FormParam(value="limit") int limit,
            @FormParam(value="start") int start) throws Exception {
        QueryAppInfoResponse response = new QueryAppInfoResponse();
        Map<String,Object> params=new HashMap<String, Object>();
        if(StringUtils.isNotBlank(appId)){
            params.put("appId", appId);
        }
        if(StringUtils.isNotBlank(appName)){
            params.put("appName", appName);
        }
        try {
            params.put("createStartTime",
                    StringUtils.isEmpty(createStartTime)?null:
                            DateUtils.parseDate(createStartTime, "yyyy-MM-dd"));
            params.put("createEndTime",
                    StringUtils.isEmpty(createEndTime)?null:
                            DateUtil.oneDateLastTime(DateUtils.parseDate(createEndTime, "yyyy-MM-dd")));
            GenericPage<AppInfoEO> genericPage =  appInfoService.queryAppInfo(params,limit,start,"create_time",false);
            response.setAppInfoList(genericPage.getData());
            response.setTotalCount(genericPage.getTotalCount());
            this.success(response);
        } catch (BusinessException e) {
            this.error(response, e);
        }
        return response;
    }


    /**
     * 增加应用信息
     * @return
     */
    @Path("addAppInfo")
    @Consumes("multipart/form-data; charset=UTF-8")
    @POST
    public IServiceResponse addAppInfo(
			@Multipart(value = "appKey", required = false) String appKey,
			@Multipart(value = "appName", required = true) String appName,
			@Multipart(value = "url", required = true) String url,
			@Multipart(value = "descInfo", required = false) String descInfo,
			@Multipart(value = "logUrl", required = false) String logUrl,
			@Multipart(value = "logoImage", required = false) byte[] logoImage,
			@Context HttpServletRequest request){
        QueryAppInfoResponse response = new QueryAppInfoResponse();
        try {
        	AppInfoEO appInfoEO =new AppInfoEO();
			if(StringUtils.isNotBlank(appKey)){
				appInfoEO.setAppKey(appKey);
			}
			if(StringUtils.isNotBlank(appName)){
				appInfoEO.setAppName(appName);
			}
			if(StringUtils.isNotBlank(url)){
				appInfoEO.setUrl(url);
			}
			if(StringUtils.isNotBlank(logUrl)){
				appInfoEO.setLogUrl(logUrl);
			}
			if(StringUtils.isNotBlank(descInfo)){
				appInfoEO.setDescInfo(descInfo);
			}
            appInfoService.addApp(appInfoEO, logoImage);
            this.success(response);
        } catch (BusinessException e) {
            this.error(response, e);
        }
        return response;
    }

    /**
	 * 修改应用信息
	 * @return
	 */
	@Path("updateAppInfo")
	 @Consumes("multipart/form-data; charset=UTF-8")
	@POST
	public IServiceResponse updateAppInfo(@Multipart(value = "appKey", required = false) String appKey,
			@Multipart(value = "id", required = false) String id,
			@Multipart(value = "appName", required = true) String appName,
			@Multipart(value = "url", required = true) String url,
			@Multipart(value = "logUrl", required = false) String logUrl,
			@Multipart(value = "descInfo", required = false) String descInfo,
			@Multipart(value = "logoImage", required = false) byte[] logoImage,
			@Context HttpServletRequest request){
		QueryAppInfoResponse response = new QueryAppInfoResponse();
		try{
			AppInfoEO appInfoEO =new AppInfoEO();
			if(StringUtils.isNotBlank(appKey)){
				appInfoEO.setAppKey(appKey);
			}
			if(StringUtils.isNotBlank(id)){
				appInfoEO.setId(Long.parseLong(id));
			}
			if(StringUtils.isNotBlank(appName)){
				appInfoEO.setAppName(appName);
			}
			if(StringUtils.isNotBlank(url)){
				appInfoEO.setUrl(url);
			}
			if(StringUtils.isNotBlank(logUrl)){
				appInfoEO.setLogUrl(logUrl);
			}
			if(StringUtils.isNotBlank(descInfo)){
				appInfoEO.setDescInfo(descInfo);
			}
			appInfoService.modifyAppInfo(appInfoEO, logoImage);
			this.success(response);
		}catch (BusinessException e) {
			this.error(response, e);
		}
		return response;
	}
	
	@Path("enableAppInfo")
	@POST
	public IServiceResponse enableAppInfo(AppInfoRequest request) {
		QueryAppInfoResponse response = new QueryAppInfoResponse();
		try{
			appInfoService.enableAppInfo(request.getId());
			this.success(response);
		}catch (BusinessException e) {
			this.error(response, e);
		}
		return response;
	}
	
	/**
	 * 禁用用户
	 * @return
	 */
	@Path("disableAppInfo")
	@POST
	public IServiceResponse disableAppInfo(AppInfoRequest request) {
		QueryAppInfoResponse response = new QueryAppInfoResponse();
		try{
			appInfoService.disableAppInfo(request.getId());
			this.success(response);
		}catch (BusinessException e) {
			this.error(response, e);
		}
		return response;
	}

	@Path("resetAppKey")
	@POST
	public IServiceResponse resetAppKey(AppInfoRequest request) {
		QueryAppInfoResponse response = new QueryAppInfoResponse();
		try{
			appInfoService.resetAppKey(request.getId());
			this.success(response);
		}catch (BusinessException e) {
			this.error(response, e);
		}
		return response;
	}

}
